<template>
  <layoutBlock >
    <div class="content">
      <div class="">
        <!--      <Select filterable v-model="condition.car_type_code" clearable placeholder="请选择类型" @on-change="getData">-->
        <!--        <Option v-for="item in statusList" :value="item.car_type_code" :key="item.car_type_code">{{ item.car_type_name }}</Option>-->
        <!--      </Select>-->
        <Input class="mb10" search v-model="condition.keyword" placeholder="请输入护栏名称" @on-search="getData" />
        <Button class="ml10 fr"  @click="getEdit('',true)"><Icon type="md-add" />新增护栏</Button>
        <Button class="ml10 fr" type="error" @click="delAll">删除</Button>
      </div>
      <Table @on-selection-change="selectList" border class="mt20" ref="selection" :columns="columns2" :data="data1">
        <template slot-scope="{row}" slot="installDate">
          {{row.installDate=="Invalid date"||!row.installDate?'-':row.installDate}}
        </template>
        <template slot-scope="{ row, index }" slot="action" >
          <span class="cursor cr2d8cf0" @click="getEdit(row,false)">编辑</span>
          <span class="cursor cr2d8cf0" @click="getDel(row,1)">删除</span>
        </template>
      </Table>
      <div class="disflex mt30">
        <Page :total="total" v-if="total>condition.limit"  :current="condition.start" show-elevator @on-change="changepage"></Page>
      </div>
      <Modal class="info-editor"  footer-hide  v-model="isShow"  width="100%">
        <infoEditor :deviceCount="deviceCount"  :orgList="orgList" :list="statusList" :isAdd="isAdd" :editorData="editorData" v-if="isShow" @success="success"></infoEditor>
      </Modal>
      <Modal
          v-model="isShow2"
          title="删除"
          @on-ok="getDel('',2)"
          @on-cancel="cancel" width="350">
        <div class="input-item">
         警告： {{message}}
        </div>
      </Modal>
    </div>
  </layoutBlock>
</template>
<script>
import infoEditor from '../../components/device/railEditor';
import {railType,railList,railDel} from '@/api/device';
import base from '@/mixins/base';
export default {
  mixins: [base],
  components:{infoEditor},
  data () {
    return {
      total:0,//总条数
      roleId:'',
      editorData:{},
      title:'编辑',
      statusList:[],
      message:'该护栏将被删除, 是否继续?',
      cureenRail:[],
      condition:{//头部筛选传参
        keyword:'',
        limit:10,
        start:1,
        is_enable:'',
      },
      columns2: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '护栏名称',
          key: 'name',
          ellipsis:true
        },
        {
          title: '创建时间',
          slot: 'installDate',
        },
        {
          title: '所属单位',
          key: 'orgName',
          ellipsis:true
        },

        {
          title: '所属项目',
          key: 'projectName',
          align: 'center',
          ellipsis:true
        },

        {
          title: '安装地址',
          key: 'address',
          ellipsis:true
        },
        {
          title: '操作',
          slot: 'action',
          width: 120,
          align: 'center'
        }
      ],
      data1: [],
      isShow:false,
      isShow2:false,
      isShow3:false,
      isShow4:false,
      detail:'',
      ids:'',
      isAdd:true,
      noShow:false,
      userId:'',
      isShowUser:false,
      selectlist:[],
      orgList:[],
      deviceCount:0,

    }
  },
  created(){
     this.getType();
    this.getData();
  },
  filters:{
    format(arr){//这里不能用类似与formatDepts 驼峰命名 不然模版中使用会编译报错
      let res= (arr||[]).map(item=>{
        return item.name
      })
      return res.join('、')
    },
  },
  methods: {


    async getType(){
      let res = await railType({});
      this.statusList= _.get(res,'data')||[];
    },
    selectList(value){
      this.condition.start=1;
      this.selectlist=value
    },
    delAll(){
      let arr= this.selectlist.map(item=>{
        return item.id
      })
      let names= this.selectlist.filter(item=>item.deviceCount>0).map(o=>o.name);
      if(arr.length==0){
        this.$Message.warning('请先选择护栏！')
        return
      }
      this.getDel({id:arr.join(),names},1)
    },
    success(){
      this.getData();
      this.isShow=false;
      this.isShow03=false;
    },
    //获取列表
    async getData(){
      let  res =!this.orgIds?this.defaultListData: await  railList({...this.condition ,pOrgId:this.orgIds});
      this.data1= _.get(res,'data.records') ||[];
      this.total=_.get(res,'data.total')||0;
    },
    //分页railType
    changepage(page){
      this.condition.start = page
      this.getData()
    },
    getEdit(row,type){
      this.isAdd=type;
      this.isShow = true;
      if(type) return;
      let {address,bz,name,id,installDate,projectId,position} = row;
      this.deviceCount=row.deviceCount;
      this.editorData={address,bz,name,id,installDate,projectId,position}
    },
    async getDel(row,type){
      if(type==1){
        this.ids = row.id;
        if(!row.names){
          this.message=row.deviceCount>0?'当前护栏上有尚未拆卸的设备，是否确认删除？':'确认删除当前护栏？'
        }else{
          this.message=row.names.length>0?`${row.names.join('、') + row.names.length }个护栏上有尚未拆卸的设备，是否确认删除？`:'确认删除当前选择的护栏？'
        }

        this.isShow2 = true;
      }else{
        let res = await railDel({projectIds:this.ids});
        if(_.get(res,'code')==0){
          this.isShow2 = false;
          this.$Message.success('删除成功')
          this.getData();
        }else{
          this.$Message.error(_.get(res,'message')||'删除失败！')
        }
      }
    },

    cancel(){},
  }
}
</script>

<style  lang="less">
.info-editor{
  .ivu-modal-content{
    height: 100vh ;
    padding-top:30px ;
  }
  .ivu-modal{
    top:0;
  }
  .ivu-input-wrapper{

  }
}
</style>
